package com.ndp.fb.mq.service.schedulerservice.thirdpictureaudit;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ndp.fb.enums.adgroupstatus.AdGroupStatus;
import com.ndp.fb.fbagent.service.AdGroupFBService;
import com.ndp.fb.model.fbagent.param.APIPriority;
import com.ndp.fb.model.fbagent.param.ApiCallBackTarget;
import com.ndp.fb.model.fbagent.param.adgroup.AdGroupParam;
import com.ndp.fb.model.fbagent.result.common.FBSingleId;
import com.ndp.fb.mq.api.service.schedulerservice.AdAccountsSchedulerService;
import com.ndp.fb.rdb.api.ThirdPicAutoAuditRdbService;
import com.ndp.fb.rdb.model.ThirdPicAutoAudit;
import com.ndp.fb.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Elvis on 10/20/2015.
 */
@Component
public class AutoCheckResourceFourStep {
    private static Logger logger = LoggerFactory.getLogger(AutoCheckResourceFourStep.class);
    @Reference
    AdGroupFBService adGroupFBService;
    @Autowired
    private AdAccountsSchedulerService adaccountsSchedulerService;
    @Reference
    private ThirdPicAutoAuditRdbService thirdPicAutoAuditRdbService;

    /**
     * 创建广告
     * @param campaignGroupId
     * @param adSetId
     * @param adCreativeId
     * @param mongdbId
     */
    public void autoPublishAd(Long campaignGroupId, Long adSetId, Long adCreativeId, String mongdbId, String orgId){
        logger.info("自动发广告autoPublishAd----begin");
        logger.info("adsetId:" + adSetId + ",campaignId:" + campaignGroupId + ",mongdbId:" + mongdbId);
        Long accountId = AutoCheckConstant.getFbAccountId();
        String accessToken = adaccountsSchedulerService.getAdaccountsToken(accountId);
        if(StringUtil.isEmpty(accessToken))
            return;
        AdGroupParam adGroupParam = new AdGroupParam();
        adGroupParam.setAccessToken(accessToken);
        adGroupParam.setFbAccountId(accountId);
        adGroupParam.setCampaignId(campaignGroupId);
        adGroupParam.setAdCreativeId(adCreativeId);
        adGroupParam.setCreative("{'creative_id':" + adCreativeId + "}");
        adGroupParam.setName("爆发吧");
        adGroupParam.setLocale("zh_CN");
        adGroupParam.setStatus(AdGroupStatus.ACTIVE.getStatus());
        adGroupParam.setAdSetId(adSetId);

        adGroupParam.setApiPriorityCode(APIPriority.CreateAdGroups.getCode());
        adGroupParam.setApiCallbackTargetType(ApiCallBackTarget.AdPublish.AutoAdGroup.getTargetType());
        adGroupParam.setCallBack(true);
        adGroupParam.setResultClass(FBSingleId.class);

        Map<String, String> equipment = new HashMap<>();
        equipment.put("adSetId", adSetId + "");
        equipment.put("mongdbId", mongdbId);
        equipment.put("orgId", orgId);
        adGroupParam.setEquipment(equipment);

        adGroupFBService.createAdGroup(adGroupParam);
    }

    public void autoPublishAdCallBack(Map<String, String> equipment, Object callBackObject) {
        logger.info("自动发广告回来autoPublishAd----callback");
        if (callBackObject != null && equipment != null) {
            String adSetId = equipment.get("adSetId");
            String mongdbId = equipment.get("mongdbId");
            String orgId = equipment.get("orgId");
            if (callBackObject instanceof FBSingleId) {
                ThirdPicAutoAudit autoAudit = new ThirdPicAutoAudit();
                autoAudit.setAdGroupId(((FBSingleId) callBackObject).getId());
                autoAudit.setAdSetId(Long.parseLong(adSetId));
                autoAudit.setOrgId(Long.parseLong(orgId));
                autoAudit.setMongodbId(mongdbId);
                autoAudit.setFlag(0);
                thirdPicAutoAuditRdbService.save(autoAudit);
            }
            else
            {
                logger.error("自动发广告失败：：" + callBackObject.toString());
            }
        } else {
            logger.info("callBackObject error!");
        }
    }
}
